﻿function Image(position, size, src, text) {
    this.image = svg.append('image')
                .attr("xlink:href", src)

    this._text = null;
    if (text != undefined && size.h > 16) {
        this._text = svg.append('text')
                    .attr("font-size", "9")
                    .attr("fill", "rgb(0,0,127)")
                    .attr("tag", text)
                    .attr("inRect", "true")
                    .text(text.split('_')[1]);
    }

    BaseNode.apply(this, [this.image, position, size]);

    this.hide = function () {
        this._domEl.attr("display", "none");
        if (this._text != null) {
            this._text.attr("display", "none");
        }
    }

    this.onUpdateLayout = function () {
        this.image.attr("x", this.getActualPosition().x)
            .attr("y", this.getActualPosition().y)
            .attr("width", this.getSize().w)
            //.attr("id", parmas.id)
            .attr("height", this.getSize().h);

        if (this.logFlag) {
            worker.postMessage("Create a Image:  x:" + this._position.x + ", y:" + this._position.x + ", width:" + this._size.w + ", height:" + this._size.h);
        }
        this.logFlag = false;
        if (this._text != null) {
            this._text.attr("x", this.getActualPosition().x + 10);
            this._text.attr("y", this.getActualPosition().y + this.getSize().h + 12);
        }
    };

    this.hitTest = function (pos) {
        var actualPosition = this.getActualPosition();
        if (pos.x > actualPosition.x
            && pos.y > actualPosition.y
            && pos.x < actualPosition.x + this.getSize().w
            && pos.y < actualPosition.y + this.getSize().h) {
            return new HitResult(true, actualPosition.x - pos.x, actualPosition.y - pos.y, this);
        }
        return new HitResult(false, 0, 0, null);
    }

    this.getCenterPos = function () {
        var x = this.getActualPosition().x + (this._size.w) / 2;
        var y = this.getActualPosition().y + (this._size.h) / 2;
        return { x: x, y: y };
    }
}